// const mongoose = require('mongoose')
// mongoose.set('strictQuery', true);
// var config=require("../config/index")
// const url = config.mongodb_url;


var config = require("../config/index")
const url = config.mongodb_url;
const dbName = config.mongodb_name;


// 引入必要的模块
const mongoose = require('mongoose');
const initIndex = require("./initIndex");
mongoose.set('strictQuery', true);

// 从环境变量中获取数据库配置
// const url = process.env.MONGODB_URL || 'mongodb://localhost:27017';
// const dbName = process.env.MONGODB_NAME || 'mydatabase';



mongoose.connect(`${url}/${dbName}`, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
}).then(() => {
    console.log('数据库连接成功');
    //初始化索引
    initIndex();


}).catch(err => {
    console.error('数据库连接失败', err);
    process.exit(1); // 退出进程并返回错误码
});
module.exports = mongoose